<template>
  <ElementDialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <ElForm
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()">
      <ElFormItem label="标题" prop="subject">
        <ElInput v-model="dataForm.subject" placeholder="标题" />
      </ElFormItem>
      <ElFormItem label="文档类型" prop="doctype">
        <ElInput v-model="dataForm.doctype" placeholder="文档类型" />
      </ElFormItem>
      <ElFormItem label="文档ID" prop="sourceid">
        <ElInput v-model="dataForm.sourceid" placeholder="文档ID" />
      </ElFormItem>
      <ElFormItem label="分类ID" prop="ctiId">
        <ElInput v-model="dataForm.ctiId" placeholder="分类ID" />
      </ElFormItem>
      <ElFormItem label="分类名称" prop="ctiName">
        <ElInput v-model="dataForm.ctiName" placeholder="分类名称" />
      </ElFormItem>
      <ElFormItem label="分类全名称" prop="ctiFullname">
        <ElInput v-model="dataForm.ctiFullname" placeholder="分类全名称" />
      </ElFormItem>
      <ElFormItem label="分类全编码" prop="ctiFullcode">
        <ElInput v-model="dataForm.ctiFullcode" placeholder="分类全编码" />
      </ElFormItem>
      <ElFormItem label="状态默认为0{0：启用、1：禁用}" prop="status">
        <ElInput v-model="dataForm.status" placeholder="状态默认为0{0：启用、1：禁用}" />
      </ElFormItem>
      <ElFormItem label="消息描述" prop="infor">
        <ElInput v-model="dataForm.infor" placeholder="消息描述" />
      </ElFormItem>
      <ElFormItem label="打开超链接" prop="openUrl">
        <ElInput v-model="dataForm.openUrl" placeholder="打开超链接" />
      </ElFormItem>
      <ElFormItem label="openType" prop="openType">
        <ElInput v-model="dataForm.openType" placeholder="openType" />
      </ElFormItem>
      <ElFormItem label="备注" prop="remarks">
        <ElInput v-model="dataForm.remarks" placeholder="备注" />
      </ElFormItem>
      <ElFormItem label="完成时限" prop="timelimitType">
        <ElInput
          v-model="dataForm.timelimitType"
          placeholder="完成时限0：长期坚持；1：周期完成。如果是周期完成就必须填写开始结束时间" />
      </ElFormItem>
      <ElFormItem label="开始时间" prop="startdate">
        <ElInput v-model="dataForm.startdate" placeholder="开始时间（精确到天）" />
      </ElFormItem>
      <ElFormItem label="结束时间" prop="enddate">
        <ElInput v-model="dataForm.enddate" placeholder="结束时间（精确到天）" />
      </ElFormItem>
      <ElFormItem label="提醒方式" prop="mode">
        <ElInput
          v-model="dataForm.mode"
          placeholder="提醒方式(支持多选格式如0,0,0；代表短信、系统消息、邮件。1代表需要，0代表不需要中)" />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <span class="dialog-footer">
        <ElButton @click="visible = false">取消</ElButton>
        <ElButton v-if="canSubmit" type="primary" @click="dataFormSubmit()">确定</ElButton>
      </span>
    </template>
  </ElementDialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/message/messagemanagement'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          unid: '',
          createId: '',
          createBy: '',
          createTime: '',
          deptId: '',
          deptFullname: '',
          updateBy: '',
          updateTime: '',
          tenantId: '',
          delFlag: '',
          subject: '',
          doctype: '',
          sourceid: '',
          ctiId: '',
          ctiName: '',
          ctiFullname: '',
          ctiFullcode: '',
          status: '0',
          infor: '',
          openUrl: '',
          openType: '',
          remarks: '',
          timelimitType: '',
          startdate: '',
          enddate: '',
          mode: ''
        },
        dataRule: {
          subject: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
          doctype: [{ required: true, message: '文档类型不能为空', trigger: 'blur' }],
          sourceid: [{ required: true, message: '文档ID不能为空', trigger: 'blur' }],
          ctiId: [{ required: true, message: '分类ID不能为空', trigger: 'blur' }],
          ctiName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
          ctiFullname: [{ required: true, message: '分类全名称不能为空', trigger: 'blur' }],
          ctiFullcode: [{ required: true, message: '分类全编码不能为空', trigger: 'blur' }],
          status: [{ required: true, message: '状态默认为0{0：启用、1：禁用}不能为空', trigger: 'blur' }],
          infor: [{ required: true, message: '消息描述不能为空', trigger: 'blur' }],
          openUrl: [{ required: true, message: '打开超链接不能为空', trigger: 'blur' }],
          openType: [{ required: true, message: 'openType不能为空', trigger: 'blur' }],
          remarks: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
          timelimitType: [
            {
              required: true,
              message: '完成时限0：长期坚持；1：周期完成。如果是周期完成就必须填写开始结束时间不能为空',
              trigger: 'blur'
            }
          ],
          startdate: [{ required: true, message: '开始时间（精确到天）不能为空', trigger: 'blur' }],
          enddate: [{ required: true, message: '结束时间（精确到天）不能为空', trigger: 'blur' }],
          mode: [
            {
              required: true,
              message: '提醒方式(支持多选格式如0,0,0；代表短信、系统消息、邮件。1代表需要，0代表不需要中)不能为空',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.canSubmit = true
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.canSubmit = false
            if (this.dataForm.id) {
              putObj(this.dataForm)
                .then(data => {
                  this.$notify.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            } else {
              addObj(this.dataForm)
                .then(data => {
                  this.$notify.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            }
          }
        })
      }
    }
  }
</script>
